<?php 
session_start();
class face extends CI_Controller  {

        /**
        * Index Page for this controller.
        *
        * Maps to the following URL
        * 		http://example.com/index.php/welcome
        *	- or -  
        * 		http://example.com/index.php/welcome/index
        *	- or -
        * Since this controller is set as the default controller in 
        * config/routes.php, it's displayed at http://example.com/
        *
        * So any other public methods not prefixed with an underscore will
        * map to /index.php/welcome/<method_name>
        * @see http://codeigniter.com/user_guide/general/urls.html
        */
        private $_configs = null;
        function __construct()
        {
            parent::__construct();
            $this->load->helper('url');
            $this->load->library('javascript');
            $this->load->library('session');
            $this->load->library('smarty3','',"smarty");
            $config['appId']='470593322973062';
            $config['secret']='bcbed04cfc25e18fcad789ea14cd755e';
            $config['fileUpload']=true;
            $this->FaceConfig["Config"]=$config;
            $this->FaceConfig["Params"]['scope'] = 'publish_stream,user_photos';
            $this->FaceConfig["Params"]['redirect_uri'] = 'http://dealgiadung.com/openid/face';
            $this->load->model('deal/customer_model','customer_model');
        }
        public function index()
        {
            require_once (APPPATH."libraries/Face/facebook.php");
            $config=$this->FaceConfig["Config"];
            $facebook = new Facebook($config);
            try{
                
            } catch (Exception $e) {
                    echo 'Lỗi:'.$e->getMessage();
            }
            try{
                $userid = $facebook->getUser();
                $paramsp=$this->FaceConfig["Params"];
                
                if($userid){
                    $_SESSION["FaceBook-User"] = $facebook->api('/me');
                    redirect(base_url("openid/face/flogin"));

                }else
                {
                    $loginUrl = $facebook->getLoginUrl($paramsp);
                    exit("Loading ...<script>window.top.location.replace('$loginUrl');</script>");

                }
               
            } catch (ApiException $e) {
                    echo 'Lỗi:'.$e->getMessage();
            }
        }
        function flogin(){
            $e=$_SESSION["FaceBook-User"]["username"]."@facebook.com";
            $_SESSION["D-USER"]=array(
                "CustomerID"=>"F".strtoupper(md5($_SESSION["FaceBook-User"]["id"])),
                "CustomerName"=>$_SESSION["FaceBook-User"]["name"],
                "Mail"=>$e,
                "Avatar"=>"https://graph.facebook.com/".$_SESSION["FaceBook-User"]["id"]."/picture",
                "Type"=>"FaceBook"
            );
            $account=$this->customer_model->get($e);
            if(count($account)==0){
                $this->customer_model->insert($_SESSION["D-USER"]);
                $account=$this->customer_model->get($e);
                $_SESSION["D-USER"]=objectToArray($account[0]);
                
                $params_mail=array("Email"=> $e);
                $this->customer_model->insertMail($params_mail);
            }else{
                $_SESSION["D-USER"]=objectToArray($account[0]);
            }
            
            $this->smarty->display("openid/registerwithopenid");
        }
        
          
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
